using Joy.Erp.BasicArchives.Core.Filters;
using Joy.Erp.BasicArchives.HttpApi.SimplyClient.Core;
using Joy.Erp.BasicArchives.Staff;
using System;
using System.Threading.Tasks;

namespace Joy.Erp.BasicArchives.HttpApi.SimplyClient.Staff
{
    public class DepartmentClientProxy
    {
        public readonly TokenClientProxy TokenClientProxy;

        public DepartmentClientProxy(TokenClientProxy tokenClientProxy)
        {
            TokenClientProxy = tokenClientProxy;
        }

        public async Task<DepartmentDto> GetAsync(Guid id, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/department/{id}");

            return await ApiHandler.HttpGetAsync<DepartmentDto>(token, url, logfunc);
        }

        public DepartmentDto Get(Guid id, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await GetAsync(id, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<DepartmentDto> FindAsync(string filter, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/department/find?filter={filter}");

            return await ApiHandler.HttpGetAsync<DepartmentDto>(token, url, logfunc);
        }

        public DepartmentDto Find(string filter, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await FindAsync(filter, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<DepartmentDto> FindBySourceKeyAsync(string sourceKey, string sourceType, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/department/find-by-source-key?sourceKey={sourceKey}&sourceType={sourceType}");

            return await ApiHandler.HttpGetAsync<DepartmentDto>(token, url, logfunc);
        }

        public DepartmentDto FindBySourceKey(string sourceKey, string sourceType, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await FindBySourceKeyAsync(sourceKey, sourceType, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<PagedResultDto<DepartmentDto>> GetListAsync(FilterRequestDto input, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/department?filter={input.Filter}&sorting={input.Sorting}&skipCount={input.SkipCount}&maxResultCount={input.MaxResultCount}");

            return await ApiHandler.HttpGetAsync<PagedResultDto<DepartmentDto>>(token, url, logfunc);
        }

        public PagedResultDto<DepartmentDto> GetList(FilterRequestDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await GetListAsync(input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<PagedResultDto<DepartmentDto>> QueryAsync(FilterGroupRequestDto input, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/department/query");

            return await ApiHandler.HttpPostAsync<PagedResultDto<DepartmentDto>>(token, url, input, logfunc);
        }

        public PagedResultDto<DepartmentDto> Query(FilterGroupRequestDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await QueryAsync(input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<ListResultDto<DepartmentDto>> GetChildrenAsync(Guid? parentId, bool recursive = false, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/department/get-children");

            var requestData = new { parentId, recursive };
            return await ApiHandler.HttpPostAsync<ListResultDto<DepartmentDto>>(token, url, requestData, logfunc);
        }

        public ListResultDto<DepartmentDto> GetChildren(Guid? parentId, bool recursive = false, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await GetChildrenAsync(parentId, recursive, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<DepartmentDto> CreateAsync(DepartmentCreateDto input, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/department");

            return await ApiHandler.HttpPostAsync<DepartmentDto>(token, url, input, logfunc);
        }

        public DepartmentDto Create(DepartmentCreateDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await CreateAsync(input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<DepartmentDto> UpdateAsync(Guid id, DepartmentUpdateDto input, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/department/{id}");

            return await ApiHandler.HttpPutAsync<DepartmentDto>(token, url, input, logfunc);
        }

        public DepartmentDto Update(Guid id, DepartmentUpdateDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await UpdateAsync(id, input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task DeleteAsync(Guid id, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/department/{id}");

            await ApiHandler.HttpDeleteAsync(token, url, logfunc);
        }

        public void Delete(Guid id, Action<string> logfunc = null)
        {
            try
            {
                Task.Run(async () => await DeleteAsync(id, logfunc));
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }
    }
} 
